<?php // jEdit :folding=indent: :collapseFolds=1: :noTabs=true:

/**
* contains class {@link PageContact}
* @author Tobias Schlatter
* @package frontEnd
* @subpackage pages
*/

/** */
require_once('Navigation.class.php');
require_once('ContactImage.class.php');
require_once('Page.class.php');
require_once('Contact.class.php');
require_once('ContactImportExport.class.php');
require_once('PluginManager.class.php');
require_once('AddressFormatter.class.php');
require_once('RightsManager.class.php');
require_once('HTMLHelper.class.php');
require_once('XSLTUtility.class.php');

/**
* the contact entry page
* 
* the contact entry page shows one
* contact at a time with all information provided
* @package frontEnd
* @subpackage pages
* @uses Contact
* @todo Remove admin change user type interface and move it to PageAdminPanel (new XSLT)
*/
class PageContact extends Page {
    
    /**
    * @var Contact the contact to show
    */
    var $contact;
    
    /**
    * @var Navigation the menu for the contact-card
    */
    var $cardMenu;
    
    /**
    * @var Navigation the menu generated by plugins
    */
    var $pluginMenu;
    
    /**
    * @var Image the mugshot of the contact
    */
    var $image;
    
    /**
    * @var Prevent display as XSLT (to change user settings or options)
    */
    var $noXSLT;
    
    /**
    * Constructor
    * 
    * init superclass, create navigation, init variables {@link $contact}, {@link $cardMenu}, {@link $pluginMenu} and {@link $image}
    * if passed value is an integer, the contact with this id is loaded, else it is considered as the contact class itself
    * @param integer|Contact $idOrContact contact, contact id when adding
    * @param boolean $enableXsltProcessing Allows to disable XSLT processing (Default: TRUE)
    * @global Options used to determine options
    * @global PluginManager used to generate plugin menu
    */
function PageContact($idOrContact,$enableXsltProcessing=TRUE) {
        global $options, $pluginManager, $errorHandler;
        
        $this->noXSLT = !$enableXsltProcessing;
        
        if (is_numeric($idOrContact))
            $this->contact = Contact::newContact($idOrContact);
        else
            $this->contact = &$idOrContact;
        
        // MANDATORY SECURITY CHECK IN CONSTRUCTOR OF EACH PAGE
        $rightsManager = RightsManager::getSingleton();
        if(!$rightsManager->currentUserIsAllowedTo('view',$this->contact))
            $errorHandler->standardError('PERMISSION_DENIED',basename($_SERVER['SCRIPT_NAME']));
        
        $this->Page($this->contact->contact['firstname'] . ' ' . $this->contact->contact['lastname']);        
    }
    
    /**
    * create the menu of the contact-card
    */
function createMenu() {

        // Create Menue
        $this->cardMenu = new Navigation('card-menu');
        $this->cardMenu->addEntry("return","return",Navigation::previousPageUrl());

        $rightsManager = RightsManager::getSingleton();
        if ($rightsManager->currentUserIsAllowedTo('edit',$this->contact))
            $this->cardMenu->addEntry("edit","edit","../contact/contact.php?mode=edit&amp;id={$this->contact->contact['id']}");
        
        $this->cardMenu->addEntry("print","print","javascript:window.print()");
        
        if ($_SESSION['user']->isAtLeast('user'))
            $this->cardMenu->addEntry("incorrect","incorrect","javascript:reportIncorrectEntry({$this->contact->contact['id']},'Are you sure you want to report this entry as incorrect? This will automatically generate an email to the person who last edited the contact.')");
        
        if ($_SESSION['user']->isAtLeast('admin') && !$this->contact->isUser())
        {
            if(!empty($this->contact->contact['xsltDisplayType']) && $this->noXSLT==FALSE)
                $this->cardMenu->addEntry('alt-view','[change view]',"../contact/contact.php?noxslt=1&amp;id={$this->contact->contact['id']}");
                
            $this->cardMenu->addEntry('cuser','[create user]',"../user/register.php?mode=cuser&amp;id={$this->contact->contact['id']}");
        }
    }
    
    
    /**
    * create the content of contact-card
    * @return string html-content
    * @global PluginManager used for custom menues
    * @global Options
    */
function innerCreate() {

        global $pluginManager, $options, $errorHandler;

        // calling script may set a new menu before calling create, otherwise create own menu
        if($this->cardMenu==null)
            $this->createMenu();
        
        // Create Plugin Menue
        $this->pluginMenu = new Navigation('card-pluginmenu');
        $pluginManager->contactMenu($this->contact,$this->pluginMenu);
        
        $this->image = new ContactImage($this->contact);
        
        // output of menu
        $menu = HTMLHelper::createNestedDivBoxModel('menu-box',
                    $this->cardMenu->create() . $this->pluginMenu->create());
        
        // is there a XSLT stylesheet to display the contact?
        if(!empty($this->contact->contact['xsltDisplayType']) && $this->noXSLT == FALSE)
        {
            $transformationFile = 'lib/xslt/' . $this->contact->contact['xsltDisplayType'] . '.xsl';
            
            if(file_exists($transformationFile))
                $cont = $this->createXSLTContact($transformationFile);
            else
                $errorHandler->warning('File not found: '.$transformationFile,basename($_SERVER['SCRIPT_NAME']));
        }
        
        if(!isset($cont))
            $cont = $this->createNormalContact();
        
        return $menu . HTMLHelper::createNestedDivBoxModel('card', $cont);
    }
    
    /**
    * creates the XSLT generated output
    */
function createXSLTContact($transformationFile)
    {
        $xml = ContactImportExport::xmlExport($this->contact);
        $x = new XSLTUtility($xml);        
        return $x->transform($transformationFile);
    }
    
    /**
    * creates the traditional output of TAB
    */
function createNormalContact()
    {
        global $pluginManager, $CONFIG_CONTACT_NAME_SPEC;
        
        if(!isset($CONFIG_CONTACT_NAME_SPEC))
            $CONFIG_CONTACT_NAME_SPEC = array(array('namePrefix',' '),array('firstname', ' '),array('middlename', ' '), 'lastname', array(', ', 'nameSuffix'));
        
        $title = HTMLHelper::createNestedDivBoxModel('card-title',$this->contact->groups() . $this->contact->generateFullName('html',$CONFIG_CONTACT_NAME_SPEC));
        
        $top = '<div class="card-top">';
        $top .= $this->image->html('card-picture');
        
        $top .= $this->contact->addresses();
        
        // Output emphomess
        $top .= '<div class="card-emphomess">';
            $top .= $this->contact->emails(NULL,NULL); // return not address associated emails
            $top .= $this->contact->phones(NULL,NULL);
            $top .= $this->contact->messaging(NULL,NULL);
        $top .= '</div></div>';
        
        $bot = '<div class="card-bottom">';
            $bot .= $this->contact->additionals(NULL,NULL);
            $bot .= $this->contact->dates(NULL,NULL);
            $bot .= $this->contact->webs(NULL,NULL);
            $bot .= $pluginManager->contactOutput($this->contact, 'beforeNotes');
            $bot .= $this->createUserInfo();
            $bot .= $this->contact->notes();
        $bot .= '</div>';
        
        $box = $title . HTMLHelper::createNestedDivBoxModel('card-box',$top . $bot);
        
        $link='';
        if(isset($this->contact->contact['whoModified']))
        {
            $who = new User(intval($this->contact->contact['whoModified']));
            if(isset($who->contact['id']))
                $link = ' by <a class="update" href="'.$_SERVER['PHP_SELF'].'?id=' . $who->contact['id'] . '">' . $who->contact['lastname'] . ', ' . $who->contact['firstname'] . '</a>';
            else
                $errorHandler->clear('argVal');
        }   
        
        $footer = '<div class="update"><span>This entry was last updated on</span> ' . $this->contact->contact['lastUpdate'] . $link . '</div></div>';
        
        return '<div class="vcard">' . $box . '</div>' . $footer;
    }
    
    /**
    * create html info, if current contact is also a user
    * @return string html-content
    * @todo find better solution than style tag in html
    */
function createUserInfo() {
        $cont = '';
        if ($this->contact->isUser()) {
            $uid = $this->contact->isUser();
            $user = new User($uid);
            if ($_SESSION['user']->contact['id'] == $this->contact->contact['id']) {
                if ($_SESSION['user']->isAtLeast('admin'))
                    $cont .= '<div class="user-info"><span class="user-info-title">User type</span><span class="user-info-info"><a href="#" onclick="effect_1 = Effect.SlideDown(\'userChanger' . $uid . '\',{duration:1.2}); return false;">' . $user->getType() . '</a></span></div>';
                else
                    $cont .= '<div class="user-info"><span class="user-info-title">User type</span><span class="user-info-info">' . $user->getType() . '</span></div>';
            } elseif ($_SESSION['user']->isAtLeast('admin')) {
                $cont .= '<div class="user-info"><span class="user-info-title">This is a user</span><span class="user-info-info"><a href="../user/options.php?userid=' . $uid . '">show/change settings</a></span></div>';
                $cont .= '<div class="user-info"><span class="user-info-title">User type</span><span class="user-info-info"><a href="#" onclick="effect_1 = Effect.SlideDown(\'userChanger' . $uid . '\',{duration:1.2}); return false;">' . $user->getType() . '</a></span></div>';
            }
            if ($_SESSION['user']->isAtLeast('admin'))
                $cont .= '<div class="user-info" id="userChanger' . $uid . '" style="display: none;"><span class="user-info-title"></span><span class="user-info-info">Change:
                    <a href="../admin/saveadmin.php?mode=chtype&amp;userid=' . $uid . '&amp;type=admin">admin</a>
                    <a href="../admin/saveadmin.php?mode=chtype&amp;userid=' . $uid . '&amp;type=manager">manager</a>
                    <a href="../admin/saveadmin.php?mode=chtype&amp;userid=' . $uid . '&amp;type=user">user</a>
                    <a href="../admin/saveadmin.php?mode=chtype&amp;userid=' . $uid . '&amp;type=guest">guest</a>
                </span></div>';
            }
        if ($cont != '')
            return '<div class="other-spacer"></div>' . $cont;
        else
            return '';
    }
    
}

?>
